package com.qf.mvc2202.exhandler;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice   // 代表当前类是统一异常处理器，并且返回的错误信息是json字符串
public class MyRestExhandler {

    /**
     *
     * @param ex
     * @return 把map中的key包含哪些信息，再注释里要写清楚
     */
    @ExceptionHandler(UserException.class)
    public Map<String,String> exhandler(UserException ex){
        System.out.println(ex);
        final HashMap<String, String> map = new HashMap<>();
        map.put("excode",ex.getExCode()+"");
        map.put("msg",ex.getExMsg());
        map.put("date",new Date().toString());
        return map;
    }

    @ExceptionHandler(Exception.class)
    public Map<String,String> exhandler(Exception ex){
        System.out.println(ex);
        final HashMap<String, String> map = new HashMap<>();
        map.put("excode","001");
        map.put("msg",ex.getMessage());
        map.put("date",new Date().toString());
        return map;
    }

}
